@extends('layouts.modal')
@section('content')
    <form class="layui-form layui-card" action="/manage/add" method="post" autocomplete="off">
        <div class="layui-card-body">
            <div class="layui-form-item">
                <label class="layui-form-label label-required-next label-required-prev">{{ trans("user.loginname") }}
                    <font color="red">*</font></label>
                <div class="layui-input-block">
                    <input name="loginname" value='{{ isset($user) ? $user->loginname : "" }}' id="loginname" required
                           placeholder="{{ trans('user.please_input_loginname') }}"
                           class="layui-input">
                    <input type="hidden" id="id" value="{{ isset($user) ? $user->id : "" }}">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">{{ trans("user.realname") }} <font color="red">*</font></label>
                <div class="layui-input-block">
                    <input name="realname" value='{{ isset($user) ? $user->realname : "" }}' id="realname" required
                           placeholder="{{ trans("user.please_input_realname") }}" class="layui-input">
                </div>
            </div>


            <div class="layui-form-item">
                <label class="layui-form-label">{{ trans("user.phone") }} <font color="red">*</font></label>
                <div class="layui-input-block">
                    <input name="phone" value='{{ isset($user) ? $user->phone : "" }}' id="phone" required
                           placeholder="{{ trans("user.please_input_phone") }}" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">{{ trans("user.email") }} <font color="red">*</font></label>
                <div class="layui-input-block">
                    <input name="email" value='{{ isset($user) ? $user->email : "" }}' id="email" required
                           placeholder="{{ trans("user.please_input_email") }}" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">{{ trans("user.access_role") }} <font color="red">*</font></label>
                <div class="layui-input-block">
                    <div class="layui-textarea">
                        @foreach($roles as $role)
                            @if($role->premission != \App\Models\AdminRolesModel::SUPERROLE)
                                <label class="think-checkbox layui-unselect">
                                    <input type="radio"
                                           {{ !empty(isset($user->roles)) && ($role->id == $user->roles || (strpos($user->roles,",") && in_array($role->id,explode(",",$user->roles)))) ? "checked": "" }}
                                           name="roles"
                                           value="{{ $role->id }}"
                                           lay-ignore="">{{ $role->name }}
                                </label>
                            @endif
                        @endforeach
                    </div>
                </div>
            </div>
            <div class="layui-form-item"></div>
            <div class="layui-form-item text-center">
                <button class="layui-btn layui-btn-sm" onclick="save()"
                        type="button">{{ trans("common.save_data") }}</button>
                <button class="layui-btn layui-btn-danger layui-btn-sm" onclick="cancel()"
                        type="button">{{ trans("common.cancel")}}</button>
            </div>
        </div>
    </form>

    <script type="text/javascript">
        function save() {
            var data = {};
            var postUrl = "";
            var loginname = $.trim($("#loginname").val());
            var realname = $.trim($("#realname").val());
            var phone = $.trim($("#phone").val());
            var email = $.trim($("#email").val());
            var id = $.trim($("#id").val());
            var roles = getChecked();

            if (loginname == "") {
                layer.msg("{{ trans("user.loginname_required") }}");
                return false;
            }
            data.loginname = loginname;
            if (realname == "") {
                layer.msg("{{ trans("user.realname_required") }}");
                return false;
            }
            data.realname = realname;
            if (phone == "") {
                layer.msg("{{ trans("user.phone_required") }}");
                return false;
            }
            data.phone = phone;
            if (email == "") {
                layer.msg("{{ trans("user.email_required") }}");
                return false;
            }
            data.email = email;
            if (roles == "") {
                layer.msg("{{ trans("user.roles_required") }}");
                return false;
            }
            data.roles = roles;
            if (id == "") {
                postUrl = "{{ url("user/add") }}";
            } else {
                data.id = id;
                postUrl = "{{ url("user/edit") }}";
            }

            $.ajax({
                url: postUrl,
                type: 'POST',
                dataType: 'json',
                headers: {
                    'X-CSRF-TOKEN': "{{ csrf_token() }}"
                },
                ContentType: "application/json; charset=utf-8",
                data: data,
                success: function (result) {
                    if (result.code == 0) {
                        layer.msg("{{ trans('common.operation_success') }}", {
                            time: 1000
                        }, function () {
                            parent.layer.closeAll();
                            parent.location.href = '{{ url("user") }}';
                        })
                    } else {
                        layer.msg(result.msg)
                    }
                }
            });
        }
        function cancel() {
            parent.layer.closeAll();
        }

        function getChecked() {
            var roles = "";
            $("input:radio[name='roles']:checked").each(function (i) {
                if (0 == i) {
                    roles = $(this).val();
                } else {
                    roles += ("," + $(this).val());
                }
            });
            return roles;
        };
    </script>
@endsection




